((global, factory) => {
  typeof exports === "object" && typeof module !== "undefined"
    ? (module.exports = factory(global))
    : typeof define === "function" && define.amd
    ? define(() => factory(global))
    : ((global =
        typeof globalThis !== "undefined" ? globalThis : global || self),
      Object.assign(global.mfox, factory(global)));
})(window, (global) => {
  const serviceWorker = global.navigator && global.navigator.serviceWorker;

  class SWStore {
    constructor(ops) {
      if (serviceWorker && ops) {
        serviceWorker.register(ops.module).then(() => {
          serviceWorker.onmessage = (e) => {
            // service-worker.js 如果更新成功会 postMessage 给页面，内容为 'sw.updateDone'
            switch (e.data) {
              case "sw.updateDone":
                ops && typeof ops.updated === "function" && ops.updated();
                break;
            }
          };

          serviceWorker.onmessageerror = (e) => {
            console.log(e);
          };
        });
      }
      return this;
    }
    update() {
      serviceWorker &&
        serviceWorker.controller &&
        serviceWorker.controller.postMessage("sw.updateDone");
    }
  }

  return {
    createSWStore(ops) {
      return new SWStore(ops);
    },
    SWStore,
  };
});
